/*
 * This file is part of connotea-java.
 *
 * connotea-java is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * connotea-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.connotea;

import static java.util.Collections.unmodifiableList;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static org.apache.commons.lang.builder.ToStringBuilder.reflectionToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Post entity bean.
 * 
 * @author <a href="mailto:christopher.townson@googlemail.com">Christopher
 *         Townson</a>
 */
public final class Post implements Comparable<Post>, Serializable {

    private static final long serialVersionUID = 1L;

    private Bookmark bookmark;

    private List<Comment> comments = new ArrayList<Comment>();

    private Date created;

    private String creator;

    private String description;

    private boolean myWork;

    private boolean privato;

    private List<Tag> subjects = new ArrayList<Tag>();

    private String title;

    private Date updated;

    private Long userBookmarkID;

    /**
     * Default, no-argument constructor.
     */
    public Post() {
        // do nothing
    }

    /**
     * Create a new Post instance using the supplied Bookmark.
     * 
     * @param bookmark the bookmark
     */
    public Post(Bookmark bookmark) {
        setBookmark(bookmark);
    }

    /**
     * Create a new Post instance using the supplied Bookmark, Comment, title,
     * myWork, and privato fields.
     * 
     * @param bookmark the bookmark
     * @param comments the comments
     * @param title the title
     * @param description the description
     * @param myWork the myWork
     * @param privato the private
     */
    public Post(Bookmark bookmark, List<Comment> comments, String title,
            String description, boolean myWork, boolean privato) {
        setBookmark(bookmark);
        setComments(comments);
        setTitle(title);
        setDescription(description);
        setMyWork(myWork);
        setPrivate(privato);
    }

    /**
     * Copy constructor.
     * 
     * @param post the post to copy
     */
    public Post(Post post) {
        setBookmark(new Bookmark(post.getBookmark()));
        setComments(post.getComments());
        setCreated(post.getCreated());
        setCreator(post.getCreator());
        setDescription(post.getDescription());
        setMyWork(post.isMyWork());
        setPrivate(post.isPrivate());
        setSubjects(post.getSubjects());
        setTitle(post.getTitle());
        setUpdated(post.getUpdated());
        setUserBookmarkID(post.getUserBookmarkID());
    }

    /**
     * Comparability is based on userBookmarkID.
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Post post) {
        return new CompareToBuilder().append(userBookmarkID,
                post.userBookmarkID).toComparison();
    }

    /**
     * Equality is based on userBookmarkID.
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Post)) {
            return false;
        }
        Post post = (Post) obj;
        return new EqualsBuilder().append(userBookmarkID, post.userBookmarkID)
                .isEquals();
    }

    /**
     * Returns the bookmark belonging to this post.
     * 
     * @return the bookmark
     */
    public Bookmark getBookmark() {
        return bookmark;
    }

    /**
     * Returns the comments associated with this post.
     * 
     * @return the comments
     */
    public List<Comment> getComments() {
        return unmodifiableList(comments);
    }

    /**
     * Returns the created date (i.e. the date the post was created on
     * Connotea).
     * 
     * @return the created date
     */
    public Date getCreated() {
        return new Date(created.getTime());
    }

    /**
     * Returns the creator (i.e. the user who created the post).
     * 
     * @return the creator
     */
    public String getCreator() {
        return creator;
    }

    /**
     * Returns the description.
     * 
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * Returns the subjects (list of tags associated with this post).
     * 
     * @return the subjects
     */
    public List<Tag> getSubjects() {
        return unmodifiableList(subjects);
    }

    /**
     * Returns the title (the user-supplied title for this post).
     * 
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * Returns the updated date (i.e. the date this post was last updated on
     * Connotea).
     * 
     * @return the updated date
     */
    public Date getUpdated() {
        return new Date(updated.getTime());
    }

    /**
     * Returns the userBookmarkID (internal Connotea ID - useful for debugging).
     * 
     * @return the userBookmarkID
     */
    public Long getUserBookmarkID() {
        return userBookmarkID;
    }

    /**
     * HashCode is based on userBookmarkID
     * 
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(userBookmarkID).toHashCode();
    }

    /**
     * Returns the myWork.
     * 
     * @return the myWork
     */
    public boolean isMyWork() {
        return myWork;
    }

    /**
     * Returns the private.
     * 
     * @return the private
     */
    public boolean isPrivate() {
        return privato;
    }

    /**
     * Sets the bookmark.
     * 
     * @param bookmark the bookmark to set
     */
    public void setBookmark(Bookmark bookmark) {
        this.bookmark = bookmark;
    }

    /**
     * Sets the comments.
     * 
     * @param comments the comments to set
     */
    public void setComments(List<Comment> comments) {
        if (isNotEmpty(comments)) {
            this.comments.addAll(comments);
        }
    }

    /**
     * Sets the created.
     * 
     * @param created the created to set
     */
    public void setCreated(Date created) {
        this.created = new Date(created.getTime());
    }

    /**
     * Sets the creator.
     * 
     * @param creator the creator to set
     */
    public void setCreator(String creator) {
        this.creator = creator;
    }

    /**
     * Sets the description.
     * 
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * Sets the myWork.
     * 
     * @param myWork the myWork to set
     */
    public void setMyWork(boolean myWork) {
        this.myWork = myWork;
    }

    /**
     * Sets the privato.
     * 
     * @param privato the privato to set
     */
    public void setPrivate(boolean privato) {
        this.privato = privato;
    }

    /**
     * Sets the subjects.
     * 
     * @param subjects the subjects to set
     */
    public void setSubjects(List<Tag> subjects) {
        if (isNotEmpty(subjects)) {
            this.subjects.addAll(subjects);
        }
    }

    /**
     * Sets the title.
     * 
     * @param title the title to set
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * Sets the updated.
     * 
     * @param updated the updated to set
     */
    public void setUpdated(Date updated) {
        this.updated = new Date(updated.getTime());
    }

    /**
     * Sets the userBookmarkID.
     * 
     * @param userBookmarkID the userBookmarkID to set
     */
    public void setUserBookmarkID(Long userBookmarkID) {
        this.userBookmarkID = userBookmarkID;
    }

    /**
     * Pretty-prints the current object state.
     * 
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return reflectionToString(this);
    }
}
